﻿using System.Collections.Generic;
using System.Web.Mvc;
using EasyMvcBlog.CustomerEngine;
using EasyMvcBlog.DTO;
using EasyMvcBlog.Domain;
using EasyMvcBlog.Service;
using System.Web.Security;
using Webdiyer.WebControls.Mvc;
using EasyMvcBlog.Data;
using System;

namespace EasyMvcBlog.Controllers
{
    public class HomeController : BaseController
    {
        ArticleDomain articleDomain = new ArticleDomain();

        public ActionResult Index(int page = 1)
        {
            int pageSize = 5;
            PagedArticleDTO pagedArticleDtos = articleDomain.GetArticleByPage(page, pageSize);
            if (!User.Identity.IsAuthenticated)
            {
                pagedArticleDtos.Articles = new PagedList<ArticleDTO>(pagedArticleDtos.Articles.FindAll(o => o.IsVisitorVisible == true), page, pageSize);
            }
            return View("Index", pagedArticleDtos);
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult ArticleByCategory(string categoryName, int page = 1)
        {
            if (string.IsNullOrEmpty(categoryName))
            {
                return RedirectToAction("Index");
            }

            CategoryDomain categoryDomain = new CategoryDomain();
            CategoryDTO searchCategory = categoryDomain.AllCategory.Find(o => o.CategoryName == categoryName);
            if (searchCategory == null)
            {
                return RedirectToAction("Index");
            }

            int categoryId = searchCategory.Id;
            List<ArticleDTO> articles = articleDomain.GetArticleByCategory(categoryId);
            PagedArticleDTO articleDTOs = new PagedArticleDTO()
            {
                Articles = new Webdiyer.WebControls.Mvc.PagedList<ArticleDTO>(articles, page, 10),
                TotalCount = articles.Count,
            };
            return View("Index", articleDTOs);
        }

        public ActionResult ArticleByTag(string tagName, int page = 1)
        {
            if (string.IsNullOrEmpty(tagName))
            {
                return RedirectToAction("Index");
            }

            TagDomain tagDomain = new TagDomain();
            TagDTO searchTag = tagDomain.AllTags.Find(o => o.TagName == tagName);
            if (searchTag == null)
            {
                return RedirectToAction("Index");
            }

            int tagId = searchTag.Id;
            List<ArticleDTO> articles = articleDomain.GetArticleByTag(tagId);
            PagedArticleDTO articleDTOs = new PagedArticleDTO()
            {
                Articles = new Webdiyer.WebControls.Mvc.PagedList<ArticleDTO>(articles, page, 10),
                TotalCount = articles.Count,
            };
            return View("Index", articleDTOs);
        }

        [HttpPost]
        public ActionResult Login(string pwd)
        {
            if (BlogDomain.CheckPwd(pwd))
            {
                FormsAuthentication.SetAuthCookie("user", true);
                return RedirectToAction("index", "admin", null);
            }

            return View();
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", new { id = 1 });
        }

        /// <summary>
        /// 接受用户的使用信息
        /// </summary>
        /// <returns></returns>
        public string AcceptSoftwareUse()
        {
            //请求参数Mac={0}&name={1}&software={2}
            string mac = Request.QueryString["Mac"];
            string name = Request.QueryString["name"];
            string software = Request.QueryString["software"];

            if (string.IsNullOrEmpty(mac) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(software))
            {
                return "false";
            }

            SoftwareUseDomain domain = new SoftwareUseDomain();
            domain.AddOrUpdateSoftwareUseInfo(new SoftwareUseDTO
            {
                ComputerMac = mac,
                ComputerName = name,
                SoftwareName = software,
            });

            return "true";
        }

        [ValidateInput(false)]
        public ActionResult AddComment(int articleId, string userName, string userMail, string userWebSite, string comment)
        {
            CommentDomain commentDomain = new CommentDomain();

            Article article = articleDomain.GetArticle(articleId);
            if (article.DenyComment)
            {
                ShowMessage("Comment is closed", MessageType.error, 7000);
                return RedirectToAction("Index", "Article", new { id = articleId });
            }

            CommentDTO dto = new CommentDTO()
            {
                CommentContent = comment,
                CreatedTime = DateTime.Now,
                Email = userMail,
                UserName = userName,
                Website = userWebSite,
                Article = article
            };
            if (commentDomain.AddComment(dto))
            {
                ShowMessage("Comment successful", MessageType.success, 5000);
            }
            else
            {
                ShowMessage("Comment failed", MessageType.error, 7000);                
            }
            return RedirectToAction("Index", "Article", new { id = articleId });
        }
    }
}
